package sm.upgrade;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.upgrade.api.UpgradePhase;
import sm.util.SMMap;

public class UserLangUpgrade implements UpgradePhase {
	private static Logger logger = LoggerFactory.getLogger(UpgradePhase.class);

	private HibernateTemplate template;

	private String language = "en";

	public void setTemplate(HibernateTemplate template) {
		this.template = template;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	@Override
	public void upgrade() {
		logger.info("Updating user languages...");

		List<Object> operators = template
				.find("from operator where syslanguage is null");
		if (operators != null) {
			for (Object operator : operators) {
				SMMap operatorMap = (SMMap) operator;
				operatorMap.put("syslanguage", language);
				template.update(operatorMap);
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("Language set for all users.");
		}

		logger.info("  Finished updating user languages.");
	}

}
